﻿var Order = {
    version: '1.0.0.0.0',
    PageIndex: 1,
    PageSize: 20,

    Init: function () {        
        
    },

    // List Lasest Orders
    LasestOrders: function () {
        $('#listLatestOrders').html('');
        var url = String.format('/GetRequest.ashx?m=order&action=listlatestorders&rand={0}', Math.random());
        $.getJSON(url, function (res) {
            if (res.Success) {
                if (res != null) {
                    $.Mustache.load('/templates/order/latestorders.htm')
                    .done(function () {
                        $('#listLatestOrders').mustache('tpl-latest-orders-list', res);
                    });
                }
            }
        });
    },

    ListSearch: function (pageIndex) {
        $('#areaOrderDetail').css('display', 'none');
        
        $('#listOrder tbody').html('<tr><td colspan="8" class="aligncenter"><img src="/images/loading.gif" alt=""/></td></tr>');
        
        $('#toTop').click();

        var keyword   = $('#txtKeyword').val();
        var startDate = $('#txtStartDate').val();
        var endDate   = $('#txtEndDate').val();

        var storeId = 0;
        $.each($('#ddlStore ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlStore select.jqTransformHidden option:eq(' + index + ')').attr('value');
                if (value > 0 && value != undefined)
                    storeId = value;
            }
        });

        var status = 0;
        $.each($('#ddlStatus ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlStatus select.jqTransformHidden option:eq(' + index + ')').attr('value');
                status = value;
            }
        });

        var adminUserId = 0;
        $.each($('#ddlAdminUser ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlAdminUser select.jqTransformHidden option:eq(' + index + ')').attr('value');
                adminUserId = value;
            }
        });

        var pageSize = Order.PageSize;
        $.each($('#ddlPageSize ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#ddlPageSize select.jqTransformHidden option:eq(' + index + ')').attr('value');
                pageSize = value;
            }
        });

        var url = String.format('/GetRequest.ashx?m=order&action=search&keyword={0}&status={1}&storeId={2}&adminUserId={3}&sd={4}&ed={5}&pi={6}&ps={7}&rand={8}',
                    keyword, status, storeId, adminUserId, startDate, endDate, pageIndex, pageSize, Math.random());

        $.getJSON(url, function (res) {
            if (res != null) {
                $('.pages').html(res.Pager);

                $.Mustache.load('/templates/order/list.htm')
                    .done(function () {
                        $('#listOrder tbody').html('');
                        $('#listOrder tbody').mustache('tpl-order-list', res);
                        $('.topDir').tipsy({ fade: true, gravity: 's' });
                        $('form').jqTransform();
                    });
            }
        });
    },

    DropDownListAdminUser: function (storeId, ddlArea, selected, event) {
        var url = String.format('/GetRequest.ashx?m=order&action=listadminuser&storeId={0}&rand={1}', storeId, Math.random());
        $.getJSON(url, function (res) {
            if (res.Success) {
                var html = '';
                html += String.format('<select  style="width: 100px" {0}>', event);

                if (res.Data.length > 0) {
                    if (storeId == 0 || storeId == -1) 
                        html += String.format('<option value="{0}">-- Tất cả --</option>', storeId);    

                    $.each(res.Data, function (i, val) {
                        if (selected > 0 && val.AdminUserId == selected)
                            html += String.format('<option value="{0}" selected="selected">{1}</option>', val.AdminUserId, val.Username);
                        else
                            html += String.format('<option value="{0}">{1}</option>', val.AdminUserId, val.Username);
                    });
                }

                html += '</select>';

                $(ddlArea).html(html);
                $('form').jqTransform();
            }
        });
    },

    DropDownListStore: function (storeId, ddlArea, selected, event) {
        var url = String.format('/GetRequest.ashx?m=order&action=liststore&storeId={0}&rand={1}', storeId, Math.random());
        $.getJSON(url, function (res) {
            if (res.Success) {
                var html = '';
                html += String.format('<select  style="width: 120px" {0}>', event);

                if (res.Data.length > 0) {
                    if (storeId == 0 || storeId == -1)
                        html += String.format('<option value="{0}">-- Tất cả --</option>', storeId);

                    $.each(res.Data, function (i, val) {
                        if (selected > 0 && val.StoreId == selected)
                            html += String.format('<option value="{0}" selected="selected">{1}</option>', val.StoreId, val.Name);
                        else
                            html += String.format('<option value="{0}">{1}</option>', val.StoreId, val.Name);
                    });
                }

                html += '</select>';

                $(ddlArea).html(html);
                $('form').jqTransform();
            }
        });
    }
};